/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

LIST_HEAD(proto_id_head);
unsigned int proto_id_list_size = 0;

struct oproto_id_st {
	struct proto_id_st *proto_id;
	struct oproto_id_st *next_representative;
} *oproto_id_ary;

struct proto_id_st *add_proto_id(struct type_id_st *return_type_idx, struct type_list_st *parameters_list) {
	struct proto_id_st *proto_id = malloc(sizeof(struct proto_id_st));
	proto_id->return_type_idx = return_type_idx;
	proto_id->parameters_list = parameters_list;
	wchar_t *shorty = malloc((1+parameters_list->nb_members)*sizeof(wchar_t));
	*shorty = *return_type_idx->str_id->str_dt->unpacked_data->data;
	int i=0;
	struct list_head *tmp;
	list_for_each(tmp, &parameters_list->type_member_head) {
		// Take a general TypeDescriptor into an array position
		shorty[++i] = *list_entry(tmp, struct type_member_st, type_member_list)->type_id->str_id->str_dt->unpacked_data->data;
		// Convert it to its ShortyDescriptor form
		if(shorty[i]==L'[')
			shorty[i]=L'L';
	}
	lgwseq_t *shorty_2;
	for_lgwseq(&shorty_2, i+1, shorty);
	proto_id->shorty_idx = add_str_dt(shorty_2);
	list_add(&proto_id->proto_id_list, &proto_id_head);
	proto_id_list_size++;
	return proto_id;
}

int oproto_id_st_compar(struct oproto_id_st *oproto_id_1, struct oproto_id_st *oproto_id_2) {
	if(oproto_id_1->proto_id->return_type_idx!=oproto_id_2->proto_id->return_type_idx) {
		return oproto_id_1->proto_id->return_type_idx-oproto_id_2->proto_id->return_type_idx;
	}
	if(oproto_id_1->proto_id->parameters_list->relative_offset!=oproto_id_2->proto_id->parameters_list->relative_offset) {
		return oproto_id_1->proto_id->parameters_list->relative_offset-oproto_id_2->proto_id->parameters_list->relative_offset;
	}
	return 0;
}

void
build_oproto_id() {
	oproto_id_ary = malloc(proto_id_list_size*sizeof(struct oproto_id_st));
	memset(oproto_id_ary, 0, proto_id_list_size*sizeof(struct oproto_id_st));
	struct list_head *tmp;
	unsigned int i=0;
	list_for_each(tmp, &proto_id_head) {
		(oproto_id_ary+i)->proto_id = (struct proto_id_st *)list_entry(tmp, struct proto_id_st, proto_id_list);
		i++;
	}
	qsort(oproto_id_ary, proto_id_list_size, sizeof(struct oproto_id_st), (int (*)(const void *, const void *))oproto_id_st_compar);
	i=1;
	unsigned int representative_i = 0, representative_idx = 0;
	while(representative_i<proto_id_list_size) {
		(oproto_id_ary+representative_i)->proto_id->idx = representative_idx;
		while(i!=proto_id_list_size && oproto_id_st_compar(oproto_id_ary+representative_i, oproto_id_ary+i)==0) {
			(oproto_id_ary+i)->proto_id->idx = (oproto_id_ary+representative_i)->proto_id->idx;
			i++;
		}
		if(i!=proto_id_list_size)
			(oproto_id_ary+representative_i)->next_representative = oproto_id_ary+i;
		representative_i=i++;
		representative_idx++;
	}
	bounds_move(NH_PROTO_ID_IDX, 3*sizeof(uint32_t)/*Storage Designators: ((struct str_id_st)(struct proto_id_st).shorty_idx).idx,
	((struct type_id_st)(struct proto_id_st).return_type_idx).idx,
	((struct type_list_st)(struct proto_id_st).parameters_list).relative_offset;
	This makes for a total of 3 uint32_t.
	*/ *representative_idx);
	majors_size_ary[NH_PROTO_ID_IDX] = representative_idx;
}

void pack_oproto_id() {
	for(struct oproto_id_st *oproto_id_representative = oproto_id_ary;oproto_id_representative!=NULL; oproto_id_representative=oproto_id_representative->next_representative){
		uint32_t packed = htole32(oproto_id_representative->proto_id->shorty_idx->idx);
		BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
		packed = htole32(oproto_id_representative->proto_id->return_type_idx->idx);
		BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
		packed = htole32(oproto_id_representative->proto_id->parameters_list->relative_offset+up_bounds_ary[NH_TYPE_LIST_IDX-1]);
		BUFFER_WRITE(buffer[NH_PROTO_ID_IDX],buf_len[NH_PROTO_ID_IDX],&packed,buf_offset[NH_PROTO_ID_IDX],sizeof(uint32_t))
	}
}
